package com.ticket.sass.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.mapper.OrderCommodityDispatchMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.common.entity.tenant.OrderCommodityDispatchEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-05-10
 */
@Service
public class OrderCommodityDispatchServiceImpl extends ServiceImpl<OrderCommodityDispatchMapper, OrderCommodityDispatchEntity> implements BaseService<OrderCommodityDispatchEntity> {
    public Map<Long, OrderCommodityDispatchEntity> getOrderCommodityDispatchMapByOrderIds(List<Long> orderIds) {
        if (orderIds.isEmpty()) {
            return new HashMap<>();
        }
        
        // 查询订单商品派发实体对象列表
        List<OrderCommodityDispatchEntity> dispatchEntities = this.list(new QueryWrapper<OrderCommodityDispatchEntity>().in("order_id_as_commodity", orderIds));
        
        // 构建订单ID到订单商品派发实体对象的映射
        Map<Long, OrderCommodityDispatchEntity> dispatchMap = new HashMap<>();
        for (OrderCommodityDispatchEntity dispatchEntity : dispatchEntities) {
            dispatchMap.put(dispatchEntity.getOrderIdAsCommodity(), dispatchEntity);
        }
        
        return dispatchMap;
    }
    
    
    public OrderCommodityDispatchEntity getOneByOrderId(Long orderId) {
        QueryWrapper<OrderCommodityDispatchEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id_as_commodity", orderId);
        queryWrapper.last("LIMIT 1");
        // 查询订单商品派发实体对象列表
        return getOne(queryWrapper);
    }
}
